/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.imitator;

import com.facebook.rebound.Spring;
import com.tumblr.backboard.Utils.MathUtils;
import ohos.agp.components.Component;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Maps an MotionEvent to an Spring,although it does not pick an property to map.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public abstract class EventImitator extends Imitator {
    /**
     * Constructor.
     *
     * @param spring the spring to use.
     * @param restValue the rest value for the spring.
     * @param trackStrategy the tracking strategy.
     * @param followStrategy the follow strategy.
     */
    public EventImitator(Spring spring, double restValue, int trackStrategy, int followStrategy) {
        super(spring, restValue, trackStrategy, followStrategy);
    }

    /**
     * Constructor. Note that the spring must be set with {@link #setSpring(Spring)}.
     *
     * @param restValue the rest value for the spring.
     * @param trackStrategy the tracking strategy.
     * @param followStrategy the follow strategy.
     */
    protected EventImitator(double restValue, int trackStrategy, int followStrategy) {
        super(restValue, trackStrategy, followStrategy);
    }

    /**
     * Called when the user touches.
     *
     * @param event the motion event
     */
    public void constrain(TouchEvent event) {
        if (mSpring != null && mFollowStrategy == FOLLOW_EXACT) {
            mSpring.setVelocity(0);
        }
    }

    /**
     * Called when the user moves their finger.
     *
     * @param offset the value offset
     * @param value the current value
     * @param delta the change in the value
     * @param dt the change in time
     * @param event the motion event
     */
    public void mime(float offset, float value, float delta, float dt, TouchEvent event) {
        if (mSpring != null) {
            mSpring.setEndValue(mapToSpring(MathUtils.floatToAdd(offset, value)));

            if (mFollowStrategy == FOLLOW_EXACT) {
                mSpring.setCurrentValue(mSpring.getEndValue());

                if (dt > 0) {
                    mSpring.setVelocity(delta / dt);
                }
            }
        }
    }

    /**
     * Called when the user releases their finger.
     *
     * @param event the motion event
     */
    public void release(TouchEvent event) {
        if (mSpring != null) {
            mSpring.setEndValue(mRestValue);
        }
    }

    /**
     * Called by an Motion imitatoror another ontouch listene when amotionevent occurs.
     *
     * @param offset the value offset
     * @param value the current value
     * @param delta the change in the value
     * @param event the motion event
     */
    protected void imitate(float offset, float value, float delta, TouchEvent event) {
        if (event != null) {
            switch (event.getAction()) {
                case TouchEvent.PRIMARY_POINT_DOWN:
                    constrain(event);
                case TouchEvent.POINT_MOVE:
                    if (event.getPointerCount() > 0) {
                        mime(offset, value, delta, event.getOccurredTime() - event.getStartTime(), event);
                    } else {
                        mime(offset, value, delta, 0, event);
                    }
                    break;
                default:
                case TouchEvent.PRIMARY_POINT_UP:
                    release(event);
                    break;
            }
        }
    }

    /**
     * Maps an user's motion to View via an Spring.
     *
     * @param view the view to perturb.
     * @param event the motion to imitate.
     */
    public abstract void imitate(Component view, TouchEvent event);
}
